<template>
  <div class="home-container">
    <!-- 搜索框 -->
    <van-nav-bar class="app-nav-bar">
      <van-button
        slot="title"
        icon="search"
        class="search-btn"
        size="small"
        type="info"
        round
        to="/search"
      >
        搜索
      </van-button>
    </van-nav-bar>

    <!-- 文章频道列表 -->
    <van-tabs 
      class="channel-tabs"
      v-model="active"
    >
      <van-tab 
        :title="channel.name"
        v-for="channel in channels"
        :key="channel.id"
      >
        <!-- 文章列表 -->
        <article-list :channel="channel"></article-list>
      </van-tab>
      <!-- 汉堡按钮定位把列表最后的位置挡住了，解决办法就是加一个和它一样大小的占位符 -->
      <div slot="nav-right" class="wap-nav-placeholder"></div>
      <div 
        slot="nav-right" 
        class="wap-nav-wrap" 
        @click="isChannelEditShow = true" 
      >
        <van-icon name="wap-nav" />
      </div>
    </van-tabs>

    <!-- 编辑频道按钮 -->
    <van-popup 
      v-model="isChannelEditShow" 
      position="bottom" 
      closeable
      close-icon-position="top-left"
      get-container="body"
      :style="{ height: '100%' }"
    >
      <!-- 模板中的 $event 表示事件参数 -->
      <channel-edit 
        :user-channels="channels" 
        :active="active"
        @close="isChannelEditShow = false"
        @update-active="active = $event"
      />
    </van-popup>
  </div>
</template>

<script> 
import { getUserChannels } from '@/api/user'
import ArticleList from './components/article-list.vue'
import ChannelEdit from './components/channel-edit.vue'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage'

export default {
  name: 'home',
  components: {
    ArticleList,
    ChannelEdit
  },
  props: {},
  data () {
    return {
      active: 0,  //被激活的列表
      channels: [], //频道列表
      isChannelEditShow: false //频道编辑层   
    }
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {},
  created () {
    this.loadChannels()
  },
  mounted () {},
  methods: {
    async loadChannels() {
      let channels = []
      if(this.user) {
        // 已经登录了，就获取线上用户频道列表
        const { data } = await getUserChannels()
        channels = data.data.channels
      } else {
        // 没有登录就访问本地频道列表
        const locakChannels = getItem('user-channels')
        // 判断本地频道列表
        if(locakChannels) {
          // 如果本地有频道列表则使用
          channels = locakChannels
        } else {
          // 用户没有登录，也没有本地储存，就请求获取默认推荐列表
          const { data } = await getUserChannels()
          channels = data.data.channels
        }
      }
      // 把处理好的数据放到 data 中以供模板使用
      this.channels = channels
    }

    // onUpdateActive(index) {
    //   this.active = index
    // }
  }
}
</script>

<style scoped lang="less">
  .home-container {
    /deep/ .van-nav-bar__title {
      max-width: none;
    }
    .search-btn {
      width: 277px;
      height: 32px;
      background: #5babfb;
      border: none;
      .van-icon {
        font-size: 16px;
      }
      .van-button__text {
        font-size: 14px;
      }
    }

    .channel-tabs {
      /deep/ .van-tabs__nav {
        padding-left: 0;
        padding-right: 0;
      }
      /deep/ .van-tab {
        border-right: 1px solid #edeff3;
        border-bottom: 1px solid #edeff3;
      }
      /deep/ .van-tabs__line {
        bottom: 20px;
        width: 15px !important;
        height: 3px;
        background: #3296fa;
      }
    }

    .wap-nav-placeholder {
      width: 33px;
      flex-shrink: 0;
    }

    .wap-nav-wrap {
    position: fixed;
    right: 0;
    width: 33px;
    height: 43px;
    background-color: #fff;
    display: flex;
    justify-content: center;
    align-items: center;
    opacity: .9;
    .van-icon {
      font-style: 24px;
    }
    &:before {
      content: '';
      width: 1px;
      height: 43px;
      background: url("./line.png") no-repeat;
      background-size: contain;
      position: absolute;
      left: 0;
      top: 0;
    }
  }

  
  }
</style>